﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Model;

namespace Control
{
    public class MGSites : ModuloDeGerenciamento<Site>, IEditavel<Site>, IDuplicavel<Site>
    {
        public MGSites()
        {
            this.dc = new Model.DataClassesDataContext();
        }

        public override List<Site> listar()
        {
            var site = from row in this.dc.Sites select row;
            List<Site> list = new List<Site>();
            foreach (Site s in site)
            {
                list.Add(s);
            }
            return list;
        }

        public override bool adicionar(Site obj)
        {
            bool flag = true;
            try
            {
                this.dc.Sites.InsertOnSubmit(obj);
                this.dc.SubmitChanges();
            }
            catch (Exception e)
            {
                this.output += "Erro ao inserir o registro";
                this.output += e.ToString();
                flag = false;
            }
            return flag;
        }

        public bool adicionar(string nome, string tipo, string categoria, string descricao)
        {
            if (string.IsNullOrEmpty(nome) || string.IsNullOrEmpty(tipo))
            {
                return false;
            }
            Site s = new Site();
            s.nome = nome;
            s.tipo = tipo;
            s.categoria = categoria;
			s.descricao = descricao;
            return this.adicionar(s);
        }
        public override bool remover(Site obj)
        {
            var flag = false;
            try
            {
                this.dc.Sites.DeleteOnSubmit(obj);
                this.dc.SubmitChanges();
            }
            catch (Exception e)
            {
                this.output += "Erro ao remover o registro";
                this.output += e.ToString();
                flag = true;
            }
            return flag;
        }

        public override Site get(uint id)
        {
            if (id == 0) return null;
            Site site = null;
            try
            {
                site = this.dc.Sites.Single(s => s.id == id);
            }
            catch (Exception e)
            {

                this.output += "Erro ao remover o registro";
                this.output += e.ToString();
                site = null;
            }
            return site;
        }
        public override bool remover(uint id)
        {
            return this.remover(this.get(id));
        }

		public bool atualizar(Site obj) {
			throw new NotImplementedException();
		}

		public bool atualizar(uint id) {
			throw new NotImplementedException();
		}
		public bool atualizar(uint id, string nome, string tipo, string categoria, string descricao) {
			throw new NotImplementedException();
		}
		public Site duplicar(Site obj) {
			throw new NotImplementedException();
		}

		public Site duplicar(uint id) {
			throw new NotImplementedException();
		}
	}
}
